/**
 * Created by james on 13-11-22.
 * download middleware
 */
var util = require('util');
var urlUtil =  require("url");
var redis = require("redis");
var events = require('events');
var tools= require('./tools');
var child_process = require('child_process');
var path = require('path');
var path = require('path');
var fs = require('fs');
var http = require('http');
var iconv = require('iconv-lite');
var BufferHelper = require('bufferhelper');
try { var unzip = require('zlib').unzip } catch(e) { /* unzip not supported */ }
var logging = require('../../lib/logging.js');
var log_level='DEBUG';
var logger = logging.getLogger('saveImage-service','abc',log_level);
var shopService=require('./ShopInfoService');
//var imageSavePath="E:\\downloadimages\\";

var downloader = function(){
    events.EventEmitter.call(this);//eventemitter inherits
    this.timeout_count = 0;
    this.settings=require('./settings');

}

//util.inherits(downloader, events.EventEmitter);//eventemitter inherits



/**
 * download page action use http request
 */
downloader.prototype.downloadItAct = function(id,url,referer,shopid){
    var self = this;
    this.imageSavePath=this.settings['save_dianping_image_path'];
    var timeOuter = false;
    var pageLink =url;
    var imgname=tools.getLastTermBySplitter(url,"/");
    var folder=shopid;
    var useProxy=this.settings['use_proxy'];
    if(useProxy){
        var proxyRouter =this.settings['proxy_router'].split(':');
        var __host =proxyRouter[0];
        var __port =proxyRouter[1];
        var __path =pageLink;
    }else
    {
        var urlobj = urlUtil.parse(pageLink);
        var __host = urlobj['hostname'];
        var __port = urlobj['port'];
        var __path = urlobj['path'];
    }
    var startTime = new Date();
    var options = {
        'host': __host,
        'port': __port,
        'path': __path,
        'method': 'GET',
        'headers': {
            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like NeoCrawler) Chrome/31.0.1650.57 Safari/537.36",
            "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Encoding":"gzip",
            "Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4",
            "Referer":referer
        }
    };
    logger.debug(util.format('Request start, %s',pageLink));
    var req = http.request(options, function(res) {
        logger.debug(util.format('Response, %s',pageLink));
        var result = {
            "remote_proxy":res.headers['remoteproxy'],
            "drill_count":0,
            "cookie":res.headers['Cookie'],
            "url":url,
        };
        result['statusCode'] = res.statusCode;
        if(parseInt(res.statusCode)==301||parseInt(res.statusCode)==302){
            if(res.headers['location']){
                result['origin']['redirect'] = urlUtil.resolve(pageLink,res.headers['location']);
                logger.debug(pageLink+' 301 Moved Permanently to '+res.headers['location']);
            }
        }
        var compressed = /gzip|deflate/.test(res.headers['content-encoding']);
        var bufferHelper = new BufferHelper();
        res.on('data', function (chunk) {
            bufferHelper.concat(chunk);
        });

        res.on('end', function (chunk) {
            if(timeOuter){
                clearTimeout(timeOuter);
                timeOuter = false;
            }
            result["cost"] = (new Date()) - startTime;
            logger.debug('download '+pageLink+', cost:'+result["cost"]+'ms');
            var page_encoding = 'auto';
            if(page_encoding==='auto'){
                page_encoding = self.get_page_encoding(res.headers);
            }
            page_encoding = page_encoding.toLowerCase().replace('\-','')
            if(!compressed || typeof unzip == 'undefined'){
                self.saveImage(id,bufferHelper.toBuffer(),folder,imgname);
            }else{
                unzip(bufferHelper.toBuffer(), function(err, buff) {
                    if (!err && buff) {
                        self.saveImage(id,buff,folder,imgname);
                    }else{
                        shopService.updateImagesDownLoadStatus(id,'','2',err);
                    }
                });
            }
        });
    });

    timeOuter = setTimeout(function(){
        if(req){
            logger.error('Cost '+((new Date())-startTime)+'ms download timeout, '+pageLink);
            req.abort();
            req=null;
            if(self.timeout_count++>5){logger.fatal('too much timeout, exit.');process.exit(1);}
        }
    },60*1000);

    req.on('error', function(e) {
        logger.error('problem with request: ' + e.message+', url:'+pageLink);
        if(timeOuter){
            clearTimeout(timeOuter);
            timeOuter = false;
        }
        if(req){
            req.abort();
            req = null;
        }
    });
    req.end();
}
/**
 * get page encoding
 * @returns {string}
 */
downloader.prototype.get_page_encoding = function(header){
    var page_encoding = 'UTF-8'
    if(header['content-type']!=undefined){
        var contentType = header['content-type'];
        var patt = new RegExp("^.*?charset\=(.+)$","ig");
        var mts = patt.exec(contentType);
        if (mts != null)
        {
            page_encoding = mts[1];
        }
    }
    return page_encoding;
}
downloader.prototype.saveImage=function(id,buff,folder,imageName){
    var relativePath=folder+path.sep;
    var file= this.imageSavePath+relativePath;
     fs.exists(file,function(exits){
        if(!exits) fs.mkdir(file);
         var dumpFile=file+imageName;
         fs.writeFile(dumpFile,buff,'utf8',function(err){
             if(err){
                 shopService.updateImagesDownLoadStatus(id,'','2',err);
                 logger.error('save image ['+relativePath+imageName+'] fali');
             }else{
                 shopService.updateImagesDownLoadStatus(id,relativePath+imageName,'1');
                 logger.log('save image ['+relativePath+imageName+'] success');
             }
         });
     });
}

var dow=new downloader();
var doStart=function(){
    shopService.getShopInfos(function(rows){
        for(i in rows){
            var reqUrl=rows[i]['image'];
            var referer=rows[i]['shopUrl'];
            var shopId=rows[i]['shopId'];
            var id=rows[i]['id'];
            dow.downloadItAct(id,reqUrl,referer,shopId);
        }
    });
}

setInterval(function(){
    doStart();
},2*1000);

